<template>
  <div>


    <!-- 搜索功能 -->
    <div class="operation-area">
      <div class="search-area">
        <el-form ref="searchForm" :model="searchForm" inline>
          <el-form-item label="题目" prop="tittle">
            <el-input
                v-model="searchForm.tittle"
                placeholder="请输入题目"
                clearable
                style="width: 300px"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
                type="primary"
                @click="handleSearch"
                icon="el-icon-search"
            >
              搜索功能
            </el-button>
            <el-button @click="reset1Form" icon="el-icon-refresh-left">
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>

    <!-- 商品列表 -->
    <el-table
        :data="listData"
        style="width: 100%"
        border
        class="goods-table"
    >
      <el-table-column
          type="index"
          label="序号"
          width="40"
          align="center"
      ></el-table-column>
      <el-table-column
          label="内容"
          min-width="180"
      >
        <template slot-scope="scope">
          <div
              class="content-ellipsis"
              @click="showFullContent(scope.row)"
          >
            {{ shortContent(scope.row.content) }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
          prop="postTime"
          label="发布时间"
          min-width="50"
          show-overflow-tooltip
      ></el-table-column>
      <el-table-column
          prop="postId"
          label="发布者Id"
          width="80"
          align="center"
      ></el-table-column>
      <el-table-column
          prop="tittle"
          label="题目"
          width="100"
          align="center"
      ></el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="primary"
              @click="editItem(scope.row)"
              class="edit-button"
          >
            编辑
          </el-button>
          <el-button
              size="mini"
              type="danger"
              @click="deleteItem(scope.row.id)"
              class="delete-button"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
          @current-change="currentChange"
          @size-change="sizeChange"
          :page-size="pageInfo.pageSize"
          :current-page="pageInfo.pageNum"
          background
          layout="prev, pager, next, sizes, total"
          :page-sizes="[8, 16, 24, 32, 40, 48]"
          :total="total"
      ></el-pagination>
    </div>

    <el-dialog
        :title="form.id ? '编辑或发布新帖子' : '编辑或发布新帖子'"
        :visible.sync="dialogVisible"
        width="700px"
        @close="resetForm"
        class="publish-dialog"
    >
      <el-form
          :model="form"
          ref="form"
          label-width="100px"
          class="publish-form"
      >
        <el-form-item
            label="内容"
            prop="content"
            :rules="[
            { required: true, message: '请输入内容', trigger: 'blur' }
          ]"
        >
          <el-input
              type="textarea"
              v-model="form.content"
              placeholder="内容"
              rows="6"
          ></el-input>
        </el-form-item>
        <el-form-item label="题目" prop="tittle">
          <el-input
              v-model="form.tittle"
              placeholder="请输入题目"
              style="width: 80%"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submit" class="submit-button">
            {{ form.id ? "更新" : "发布" }}
          </el-button>
          <el-button @click="resetForm" class="reset-button">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 查看完整内容弹窗 -->
    <el-dialog
        title="完整内容"
        :visible.sync="contentDialogVisible"
        width="600px"
    >
      <div class="full-content">{{ currentContent }}</div>
    </el-dialog>
  </div>
</template>

<script>


import Utils from "@/utils/utils";

export default {
  name: "MyGoodsManage",
  data() {
    return {
      description: "",
      contactInfo: "",
      searchForm: {},
      dialogVisible: false,
      contentDialogVisible: false,
      searchQuery: "",
      total: 0,
      pageInfo: {
        pageNum: 1,
        pageSize: 8
      },
      form: {
        postId: Utils.getUserInfo().id,
        username:Utils.getUserInfo().username
      },
      listData: [],
      currentContent: "",
      currentRow: null
    };
  },
  created() {
    this.getPageList();
  },
  methods: {
    submit() {
      if (!this.form.id) {
        this.$axios
            .post("/posts/add", this.form)
            .then((res) => {
              if (res.code === 200) {
                this.$message({
                  message: res.msg,
                  type: "success"
                });
                this.getPageList();
                this.dialogVisible = false;
              }
            });
      } else {
        this.$axios
            .put("/posts/update", this.form)
            .then((res) => {
              if (res.code === 200) {
                this.$message({
                  message: res.msg,
                  type: "success"
                });
                this.getPageList();
                this.dialogVisible = false;
              }
            });
      }
    },
    shortContent(content) {
      return content.length > 99 ? content.substring(0, 99) + "..." : content;
    },
    showFullContent(row) {
      this.currentContent = row.content;
      this.currentRow = row;
      this.contentDialogVisible = true;
    },
    getPageList() {
      let data = Object.assign(this.searchForm, this.pageInfo);
      data.postId = Utils.getUserInfo().id;
      this.$axios
          .get("/posts/page", {
            params: data
          })
          .then((res) => {
            this.listData = res.data.list;
            this.total = res.data.total;
          });
    },
    resetForm() {
      this.$refs.form.resetFields();
      this.form.id = null;
    },
    currentChange(e) {
      this.pageInfo.pageNum = e;
      this.getPageList();
    },
    sizeChange(e) {
      this.pageInfo.pageSize = e;
      this.getPageList();
    },
    handleSearch() {
      this.pageInfo.pageNum = 1;
      this.getPageList();
    },
    reset1Form() {
      this.$refs["searchForm"].resetFields();
    },
    editItem(row) {
      this.form = { ...row };
      this.dialogVisible = true;
    },
    deleteItem(id) {
      this.$confirm(
          "此操作将永久删除该帖子, 是否继续?",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
      )
          .then(() => {
            this.$axios
                .post("/posts/delete", { id })
                .then((res) => {
                  if (res.code === 200) {
                    this.$message({
                      type: "success",
                      message: "删除成功!"
                    });
                    this.getPageList();
                  }
                });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除"
            });
          });
    }
  }
};
</script>

<style scoped>
:root {
  --primary-color: #409eff;
  --success-color: #67c23a;
  --danger-color: #f56c6c;
  --text-primary: #303133;
  --text-regular: #606266;
  --border-color: #ebeef5;
  --background-color: #f5f7fa;
  --card-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell;
  color: var(--text-primary);
  background: var(--background-color);
  margin: 0;
  padding: 0;
}

.welcome-message {
  text-align: center;
  padding: 20px;
  margin-bottom: 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: var(--card-shadow);
}

.welcome-message h1 {
  font-family: "Arial", sans-serif;
  color: var(--text-primary);
  font-size: 24px;
  margin-bottom: 10px;
  font-weight: 600;
}

.operation-area {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  background: #fff;
  padding: 15px;
  border-radius: 8px;
  box-shadow: var(--card-shadow);
}

.search-area {
  flex: 1;
}

.action-area {
  display: flex;
  align-items: center;
}

.publish-button {
  margin-left: 10px;
}

.goods-table {
  margin-bottom: 20px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: var(--card-shadow);
}

.pagination {
  text-align: center;
  margin-top: 20px;
}

.el-table {
  border-radius: 8px;
  overflow: hidden;
}

.el-table th {
  background: #f8f9fa !important;
  font-weight: 600;
}

.el-table td {
  transition: background 0.3s ease;
}

.el-table .cell {
  padding: 12px 0;
}

.edit-button {
  margin-right: 10px;
}

.delete-button {
  margin-left: 10px;
}

.publish-dialog {
  border-radius: 8px;
  box-shadow: var(--card-shadow);
}

.publish-form {
  padding: 0 20px;
}

.submit-button {
  margin-right: 10px;
}

.reset-button {
  margin-left: 10px;
}

.content-ellipsis {
  cursor: pointer;
  color: var(--primary-color);
  text-decoration: underline;
}

.full-content {
  white-space: pre-wrap;
  word-wrap: break-word;
}
</style>